//
//  CardBackView.m
//  SmartCheckout
//
//  Created by Barak Edry on 1/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CardBackView.h"
#import <QuartzCore/QuartzCore.h>

@implementation CardBackView
@synthesize backgroundImage;
@synthesize nameInput;
@synthesize doneButton;
@synthesize changePINButton;
@synthesize deleage;
@synthesize card;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void) awakeFromNib {
    [super awakeFromNib];
    card = nil;
    deleage = nil;
    self.layer.opaque = YES;
    self.layer.cornerRadius = 13;
    self.layer.borderWidth = 0;
    self.layer.borderColor = [[UIColor colorWithRed:0.35f green:0.35f blue:0.35f alpha:1.0f] CGColor];
    
    backgroundImage.layer.cornerRadius = 13;
    backgroundImage.clipsToBounds = YES;

    doneButton.clipsToBounds = YES;
    changePINButton.clipsToBounds = YES;

    doneButton.layer.cornerRadius = 10;
    changePINButton.layer.cornerRadius = 10;    
    
    self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    self.layer.shouldRasterize = YES;
    

    
}


- (void)dealloc {
    self.card = nil;
}

- (IBAction)done:(id)sender {
    [self.nameInput resignFirstResponder];
    
    card.backgroundColor = backgroundImage.backgroundColor;
    
    if (deleage != nil) {
        [deleage cardEditingDidFinish];
    }
}

- (void) setCard:(Card*)cardToSet {
    card=nil;
    card = cardToSet;
    [self update];
}

- (void) update {   
    
    if (card == nil) {
        return;
    }
        self.backgroundImage.backgroundColor = card.backgroundColor;


}

+ (CardBackView*) backViewWithCard:(Card*)cardToSet {
    CardBackView* backView= nil;
    NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CardBackView" owner:nil options:nil];
    for (id currObject in topLevelObjects) {
        if ([currObject isKindOfClass:[CardBackView class]]) {
            backView = (CardBackView*)currObject;
            break;
        }
    }
    
    backView.card = cardToSet;
    return backView;
}

- (IBAction)colorSelected:(id)sender {
    UIButton* colorButton = sender;
    backgroundImage.backgroundColor = colorButton.backgroundColor;
#warning check this
    //_reflection.image = [self reflectedImage];
}

@end
